#include <stdio.h>
#include <windows.h>
#include <direct.h>
#include <winuser.h>
// #include <string.h>

int main() {

  DWORD startTime = GetTickCount();
  // printf("\n start at: %d", startTime);

  DWORD sleep = 3000;
  // Sleep(sleep);

  DWORD nowTime = GetTickCount();
  DWORD diff = nowTime - startTime;
  // printf("\noffset %d", diff);

  // char pwd[1000];
  char* pwd = _getcwd(NULL, 0);
  printf("\nCurrent Dir %s", pwd);

  free(pwd);

  // MessageBox(NULL, "Title", "Test", 0);
  HKEY hKey;
  //sap\\shell\\open\\command
  //HKEY_CLASSES_ROOT
  long ret0 = RegOpenKey(HKEY_CURRENT_USER, "sap", &hKey);
  if(ret0!=ERROR_SUCCESS)//如果无法打开hKEY,则中止程序的执行
  {
      // MessageBox(NULL, "Can Not Open sap", "Error", 0);
      DWORD dwDisp;
      HKEY sapHKey;
      RegCreateKeyEx(HKEY_CLASSES_ROOT,"sap",0, NULL, REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS, NULL, &sapHKey, &dwDisp);
      //
      RegSetValue(sapHKey,NULL,REG_SZ, "Genaral Call", strlen("Genaral Call"));
      // RegSetValue(sapHKey,"URL Protocol",REG_SZ, "", 0);
      RegSetValueEx(sapHKey,"URL Protocol",0, REG_SZ, "", 0);
      HKEY defhKey;
      RegCreateKeyEx(HKEY_CLASSES_ROOT,"sap\\DefaultIcon",0, NULL, REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS, NULL, &defhKey, &dwDisp);




      printf("\n Create Key success");

      // return 0;
  } else {
    // MessageBox(NULL, "Open Reg sap\\shell", "Success", 0);
  }
  DWORD dwLen;
  RegCreateKeyEx(HKEY_CLASSES_ROOT,"sap\\shell\\open\\command",0, NULL, REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS, NULL, &hKey, &dwLen);
  // CStr s;
  // printf("\nKey %d", hKey);

  // BYTE getValue[1000];
  // DWORD keyType = REG_SZ;//定义数据类型
  // DWORD DataLen = 1000;//定义数据长度
  // char keyName[] = "TestValue";
  // // int strUser = _T("UserName");//要查询的键名称
  // // long ret1=RegQueryValueEx(hkey,strUser,NULL,&keyType,getValue,&DataLen);
  // long ret1 = RegQueryValueEx(hKey, NULL, NULL, &keyType, getValue, &DataLen);
  // if(ret1!=ERROR_SUCCESS)
  // {
  //   // MessageBox(NULL, "Can Not Open", "Error", 0);
  //   // HKEY hKey2;
  //   // RegCreateKey(HKEY_CLASSES_ROOT,"sap\\shell\\open\\command",&hKey2);
    char* cmd = "cmd /v:on /k set m=%1 &&call set n=%%m:sap://=%%&call set n=%%n:,= %% && echo !n!";
    size_t LEN = strlen(cmd);
    RegSetValue(hKey,NULL,REG_SZ, cmd, strlen(cmd));//指定默认项或没名字的
  //   // RegSetValueEx(hKey, NULL, NULL, REG_SZ, "test", &DataLen);
  //   printf("set key ok");

  //   // RegSetValueEx(hKey2,"age",0,REG_DWORD,(CONST BYTE*)&DataLen,4);

  //   return 0;
  // } else {
  //   printf("\nKey Value %s", getValue);
  // }
  // RegCloseKey(hKey);
  MessageBox(NULL, "Install success", "Install", 0);
  return 0;
}